package com.hotelsystem.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotelsystem.pojo.Customer;
import com.hotelsystem.service.CustomerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户控制层
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    private CustomerService customerService;

    /**
     *  根据条件查询信息
     * @param customer
     * @param pageNum
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/list")
    public String findCustomerList(Customer customer, @RequestParam("pageNum")Integer pageNum, Model model, HttpSession session){
        try {
            //设置分页的页数和每页数量
            PageHelper.startPage(pageNum,5);
            //查询条件
            //创建map集合
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("name",customer.getName());
            map.put("sex",customer.getSex());
            map.put("phone",customer.getPhone());
            map.put("idNumber",customer.getIdNumber());
            List<Customer> list = customerService.findCustomerList(map);
            //添加一条模拟数据，（用来点击修改模态框的时候不为空）
            model.addAttribute("emp",new Customer(null,"sss","男","18625893214","广白云区州市",new Date(),"542222199902145896","二代身份证","汉族",null,2));
            model.addAttribute("map",map);
            //model.addAttribute("list",list);
            //获取pageInfo对象
            PageInfo<Customer> pageInfo = new PageInfo<>(list);
            model.addAttribute("pageInfo",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "system/tenantPage/tenantList";
    }

    /**
     * 删除信息
     * @param customerId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public  String delCustomer(Integer customerId){
        //定义map集合
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            //调用删除方法
            customerService.deleteCustomer(customerId);
            map.put("success","true");
        } catch (Exception e) {
            map.put("success","false");
            e.printStackTrace();
        }
        return JSONUtils.toJSONString(map);
    }
    /**
     * 根据id查询
     */
    @ResponseBody
    @RequestMapping(value = "/emp/{customerId}")
    public Customer findCustomerById(@PathVariable("customerId") Integer customerId, Model model) {

        //调用查询id方法
        Customer customer = customerService.findCustomerById(customerId);

        return customer;//返回对象
    }

    /**
     * 修改数据
     * @param customer
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/update",method= RequestMethod.POST)
    public String updateCustomer(Customer customer){
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            //调用修改方法
            int count = customerService.updateCustomer(customer);
            if (count > 0){
                map.put("success",true);//成功
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success",false);//失败
        }
        return JSONUtils.toJSONString(map);
    }

}
